Ext.define('SKJF.controller.TERIMABARANGDET',{
	extend: 'Ext.app.Controller',
	views: ['PEMBELIAN.v_terimabarangdet','PEMBELIAN.v_terimabarangdet_form'],
	models: ['m_terimabarangdet'],
	stores: ['s_terimabarangdet'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listterimabarang',
		selector: 'Listterimabarang'
	}, {
		ref: 'Listterimabarangdet',
		selector: 'Listterimabarangdet'
	}, {
		ref: 'v_terimabarangdet_form',
		selector: 'v_terimabarangdet_form'
	}, {
		ref: 'Saveterimabarangdet',
		selector: 'v_terimabarangdet_form #save'
	}, {
		ref: 'Createterimabarangdet',
		selector: 'v_terimabarangdet_form #create'
	}, {
		ref: 'TERIMABARANGDET',
		selector: 'TERIMABARANG #south'
	}],


	init: function(){
		this.control({
			'TERIMABARANGDET': {
				'afterrender': this.terimabarangdetAfterRender
			},
			'v_terimabarangdet_form': {
				'afterlayout': this.terimabarangdetAfterLayout
			},
			'Listterimabarangdet': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListterimabarangdet
			},
			'Listterimabarangdet button[action=create]': {
				click: this.createRecord
			},
			'Listterimabarangdet button[action=delete]': {
				click: this.deleteRecord
			},
			'Listterimabarangdet button[action=print]': {
				click: this.printRecords
			},
			'v_terimabarangdet_form button[action=save]': {
				click: this.saveV_terimabarangdet_form
			},
			'v_terimabarangdet_form button[action=create]': {
				click: this.saveV_terimabarangdet_form
			},
			'v_terimabarangdet_form button[action=cancel]': {
				click: this.cancelV_terimabarangdet_form
			}
		});
	},
	
	terimabarangdetAfterRender: function(){
		// var terimabarangdetStore = this.getListterimabarangdet().getStore();
		// terimabarangdetStore.load();
	},
	
	terimabarangdetAfterLayout: function(){
		this.getV_terimabarangdet_form().down('#KODE_BARANG_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListterimabarangdet	= this.getListterimabarangdet();
		var getV_terimabarangdet_form= this.getV_terimabarangdet_form(),
			form			= getV_terimabarangdet_form.getForm();
		var getSaveterimabarangdet	= this.getSaveterimabarangdet();
		var getCreateterimabarangdet	= this.getCreateterimabarangdet();
		var sel_terimabarang = this.getListterimabarang().getSelectionModel().getSelection()[0];
		
		/* grid-panel */
		getListterimabarangdet.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_terimabarangdet_form.down('#NOMOR_LPB_field').setReadOnly(true);
		getV_terimabarangdet_form.down('#KODE_BARANG_field').setReadOnly(true);
		
		if(sel_terimabarang != null){
			getV_terimabarangdet_form.down('#NOMOR_LPB_field').setValue(sel_terimabarang.data.NOMOR_LPB);
			getV_terimabarangdet_form.down('#NOMOR_PO_field').setValue(sel_terimabarang.data.NOMOR_PO);
		}
		
		getSaveterimabarangdet.setDisabled(true);
		getCreateterimabarangdet.setDisabled(false);
		getV_terimabarangdet_form.setDisabled(false);
		
		this.getTERIMABARANGDET().setActiveTab(getV_terimabarangdet_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListterimabarangdet().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListterimabarangdet: function(me, record, item, index, e){
		var getTERIMABARANGDET		= this.getTERIMABARANGDET();
		var getListterimabarangdet	= this.getListterimabarangdet();
		var getV_terimabarangdet_form= this.getV_terimabarangdet_form(),
			form			= getV_terimabarangdet_form.getForm();
		var getSaveterimabarangdet	= this.getSaveterimabarangdet();
		var getCreateterimabarangdet	= this.getCreateterimabarangdet();
		
		getSaveterimabarangdet.setDisabled(false);
		getCreateterimabarangdet.setDisabled(true);
		getV_terimabarangdet_form.down('#NOMOR_LPB_field').setReadOnly(true);getV_terimabarangdet_form.down('#KODE_BARANG_field').setReadOnly(true);		
		getV_terimabarangdet_form.loadRecord(record);
		
		getListterimabarangdet.setDisabled(true);
		getV_terimabarangdet_form.setDisabled(false);
		getTERIMABARANGDET.setActiveTab(getV_terimabarangdet_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListterimabarangdet().getStore();
		var selection = this.getListterimabarangdet().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "NOMOR_LPB" = "'+selection.data.NOMOR_LPB+'","KODE_BARANG" = "'+selection.data.KODE_BARANG+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListterimabarangdet().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_terimabarangdet/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/terimabarangdet.html','terimabarangdet_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_terimabarangdet_form: function(){
		var getTERIMABARANGDET		= this.getTERIMABARANGDET();
		var getListterimabarangdet 	= this.getListterimabarangdet();
		var getV_terimabarangdet_form= this.getV_terimabarangdet_form(),
			form			= getV_terimabarangdet_form.getForm(),
			values			= getV_terimabarangdet_form.getValues();
		var store 			= this.getStore('s_terimabarangdet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_terimabarangdet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('NOMOR_LPB') === values.NOMOR_LPB && record.get('KODE_BARANG') === values.KODE_BARANG) {
										return true;
									}
									return false;
								}
							);
							/* getListterimabarangdet.getView().select(recordIndex); */
							getListterimabarangdet.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_terimabarangdet_form.setDisabled(true);
					getListterimabarangdet.setDisabled(false);
					getTERIMABARANGDET.setActiveTab(getListterimabarangdet);
				}
			});
		}
	},
	
	createV_terimabarangdet_form: function(){
		var getTERIMABARANGDET		= this.getTERIMABARANGDET();
		var getListterimabarangdet 	= this.getListterimabarangdet();
		var getV_terimabarangdet_form= this.getV_terimabarangdet_form(),
			form			= getV_terimabarangdet_form.getForm(),
			values			= getV_terimabarangdet_form.getValues();
		var store 			= this.getStore('s_terimabarangdet');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_terimabarangdet/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_terimabarangdet_form.setDisabled(true);
					getListterimabarangdet.setDisabled(false);
					getTERIMABARANGDET.setActiveTab(getListterimabarangdet);
				}
			});
		}
	},
	
	cancelV_terimabarangdet_form: function(){
		var getTERIMABARANGDET		= this.getTERIMABARANGDET();
		var getListterimabarangdet	= this.getListterimabarangdet();
		var getV_terimabarangdet_form= this.getV_terimabarangdet_form(),
			form			= getV_terimabarangdet_form.getForm();
			
		form.reset();
		getV_terimabarangdet_form.setDisabled(true);
		getListterimabarangdet.setDisabled(false);
		getTERIMABARANGDET.setActiveTab(getListterimabarangdet);
	}
	
});